package edu.dayu.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.github.pagehelper.PageInfo;
import edu.dayu.pojo.ClubPerson;
import edu.dayu.pojo.Image;
import edu.dayu.service.ClubPersonService;
import edu.dayu.service.ImageService;
import edu.dayu.util.ClubException;
import edu.dayu.util.Result;
import edu.dayu.util.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/admin")
public class FileController {
    @Autowired
    private ImageService imageService;
    @Autowired
    private ClubPersonService clubPersonService;

    @PostMapping("/upLoadFile")
    public Result upLoadFile(@RequestPart("image")MultipartFile image,String fileName) {
        if (!image.isEmpty()){
            return Result.ok(imageService.upload(image,fileName));
        }else {
            throw new ClubException(ResultCodeEnum.FAIL);
        }
    }

    @PostMapping("/ImageList")
    public PageInfo<Image> ImageList(Image image){
        return imageService.ImageList(image);
    }

    @PostMapping("/ImageUpdate")
    public void ImageUpdate(Image image){
        imageService.ImageUpdate(image);
    }

    @PostMapping("/ImageInsert")
    public void ImageInsert(Image image){
        imageService.ImageInsert(image);
    }

    @PostMapping("/ImageDelete")
    public void ImageDelete(Image image){
        imageService.ImageDelete(image);
    }


    @PostMapping("/readExcel")
    public void readExcel(@RequestPart("personFile") MultipartFile personFile,@RequestPart("c_id") String c_id) throws IOException {
        List<ClubPerson> personList = new ArrayList<>();
        EasyExcel.read(personFile.getInputStream(), ClubPerson.class, new AnalysisEventListener<ClubPerson>() {
            @Override
            public void invoke(ClubPerson o, AnalysisContext analysisContext) {
                personList.add(o);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        }).sheet().doRead();
        if (personList.size()>0)
            clubPersonService.clubPersonMultiInsert(personList,c_id);
    }

    @RequestMapping("/writeExcel")
    public void writeExcel(String c_id, HttpServletResponse response) throws UnsupportedEncodingException {
        ClubPerson clubPerson = new ClubPerson();
        clubPerson.setC_id(new Integer(c_id));
        List<ClubPerson> personList = clubPersonService.clubPersonList(clubPerson).getList();
        String fileName = "社团成员表.xlsx";
        if (personList!=null){
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"),"ISO8859-1"));
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            try {
                OutputStream os = response.getOutputStream();
                EasyExcel.write(os,ClubPerson.class).sheet().doWrite(personList);
                os.flush();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
